package com.ruoyi.Goover.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.Course.domain.StudyCourse;
import com.ruoyi.Course.mapper.StudyCourseMapper;
import com.ruoyi.Goover.domain.SE2;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.Goover.domain.StudyEnglish;
import com.ruoyi.Goover.service.IStudyEnglishService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * GooverController
 * 
 * @author luobom
 * @date 2021-12-02
 */
@RestController
@RequestMapping("/Goover/English")
public class StudyEnglishController extends BaseController
{
    @Autowired
    private IStudyEnglishService studyEnglishService;
    @Autowired
    private StudyCourseMapper studyCourseMapper;

    /**
     * 查询Goover列表
     */
    @PreAuthorize("@ss.hasPermi('Goover:English:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudyEnglish studyEnglish)
    {
        if(studyEnglish.getPageNum1()!=null){
        studyEnglish.setPageNum1((studyEnglish.getPageNum1()-1)*studyEnglish.getPageSize1());}
        List<StudyEnglish> list = studyEnglishService.selectStudyEnglishList(studyEnglish);
        List<SE2> list2 = new ArrayList<>();
        for(StudyEnglish study:list){
            SE2 s=new SE2(study.getId(),study.getUid(),study.getUnit(),study.getWord(),study.getCx(),study.getSy(),study.getGoover(),new ArrayList<>());
            s.setRemark(study.getRemark());
            if(StringUtils.isNotEmpty(study.getSearchValue())){
                List<String> str= new ArrayList<>(Arrays.asList(study.getSearchValue().split("\\|")));
                for(String st:str){
                    s.getSearchValue().add(st);
                }
            }
            list2.add(s);
        }
        studyEnglish.setPageNum1(null);
        list = studyEnglishService.selectStudyEnglishList(studyEnglish);
        return getDataTable(list2,(long)list.size());
    }

    /**
     * 导出Goover列表
     */
    @PreAuthorize("@ss.hasPermi('Goover:English:export')")
    @Log(title = "Goover", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudyEnglish studyEnglish)
    {
        List<StudyEnglish> list = studyEnglishService.selectStudyEnglishList(studyEnglish);
        ExcelUtil<StudyEnglish> util = new ExcelUtil<StudyEnglish>(StudyEnglish.class);
        util.exportExcel(response, list, "Goover数据");
    }

    /**
     * 获取Goover详细信息
     */
    @PreAuthorize("@ss.hasPermi('Goover:English:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(studyEnglishService.selectStudyEnglishById(id));
    }

    /**
     * 新增Goover
     */
    @PreAuthorize("@ss.hasPermi('Goover:English:add')")
    @Log(title = "Goover", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SE2 se)
    {
        //获取当前课程
        StudyCourse sc=studyCourseMapper.GetDefaultCourse(String.valueOf(SecurityUtils.getUserId()));
        se.setCourseuid(sc.getUid());

        se.setGoover((long)0);
        StringBuffer uid2=new StringBuffer();
        if(se.getSearchValue()==null){se.setSearchValue(new ArrayList<>());}
        for(String uid:se.getSearchValue()){
            uid2.append(uid+"|");
        }

        StudyEnglish studyEnglish=new StudyEnglish(se.getUnit(),se.getWord(),se.getSy(),se.getGoover(), uid2.toString());
        studyEnglish.setRemark(se.getRemark());
        studyEnglish.setCourseuid(se.getCourseuid());
        return toAjax(studyEnglishService.insertStudyEnglish(studyEnglish));
    }

    /**
     * 修改Goover
     */
    @PreAuthorize("@ss.hasPermi('Goover:English:edit')")
    @Log(title = "Goover", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SE2 se)
    {
        StringBuffer uid2=new StringBuffer();
        for(String uid:se.getSearchValue()){
            uid2.append(uid+"|");
        }
        StudyEnglish studyEnglish=new StudyEnglish(se.getId(),se.getUid(),se.getUnit(),se.getWord(),se.getCx(),se.getSy(),se.getGoover(),uid2.toString());
        studyEnglish.setRemark(se.getRemark());
        studyEnglish.setCourseuid(se.getCourseuid());
        return toAjax(studyEnglishService.updateStudyEnglish(studyEnglish));
    }

    /**
     * 删除Goover
     */
    @PreAuthorize("@ss.hasPermi('Goover:English:remove')")
    @Log(title = "Goover", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(studyEnglishService.deleteStudyEnglishByIds(ids));
    }
}
